<template>
    <div>
        <basic-container>
            <div class="app-container">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                    <el-form-item label="日期查询">
                        <el-date-picker v-model="value" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期">
                        </el-date-picker>
                    </el-form-item>

                    <el-form-item label="产品SN">
                        <el-input v-model="queryParams.sequence" placeholder="请输入产品SN" clearable @clear="getList" />
                    </el-form-item>
                    <el-form-item>
                        <el-form-item label="设备名称">
                            <el-select v-model="queryParams.facility_id" placeholder="请选择" style="width: 100%;">
                                <el-option v-for="item in imagelist" :key="item.facility_id" :label="item.name"
                                :value="item.facility_id">
                            </el-option>
                            </el-select>
                        </el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-row :gutter="10" class="mb8">

            </el-row>
            <el-table :data="menuList" style="width: 100%">
                <el-table-column label="设备名称" prop="facility_name" align="center">
                </el-table-column>
                <el-table-column label="图片" prop="image" align="center">
                    <template slot-scope="scope">
                        <img 
                            :src="'data:image/jpeg;base64,' + scope.row.image" 
                            style="max-width: 100px; max-height: 100px; cursor: pointer"
                            @click="handlePreview(scope.row.image)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="产品SN" prop="sequence" align="center">
                </el-table-column>
                <el-table-column label="磁盘保存路径" prop="path" align="center">
                </el-table-column>
                <el-table-column label="操作时间" prop="time" align="center" :show-overflow-tooltip="true">
                </el-table-column>



            </el-table>
            <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
                <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                    <el-row>
                        <el-col :span="19">
                            <el-form-item label="是否合格" prop="mark">
                                <el-select v-model="form.mark" placeholder="请选择" style="width: 100%;">
                                    <el-option v-for="item in options" :key="item.value" :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="19">
                            <el-form-item label="报废信息" prop="message">
                                <el-input v-model="form.message" placeholder="请输入报废信息" :disabled="form.mark !== 3" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="19" v-if="form.mark == 2">
                            <el-form-item label="加工工艺路线" prop="routes">
                                <el-table :data="dataList" style="width: 100%">
                                    <el-table-column prop="sequence" label="加工顺序">
                                    </el-table-column>
                                    <el-table-column prop="name" label="加工设备名称">
                                    </el-table-column>
                                    <el-table-column prop="serials" label="加工设备编号">
                                    </el-table-column>
                                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

                                        <template slot-scope="scope">
                                            <el-button size="mini" type="text" icon="el-icon-delete"
                                                @click="handleDatalist(scope.$index)">
                                                删除</el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-form-item>
                            <div style="display: flex;margin-left: 15%;">
                                <div>
                                    <h3>加工顺序</h3>
                                    <el-select v-model="value" placeholder="请选择">
                                        <el-option v-for="item in sequenceOptions" :key="item.value" :label="item.label"
                                            :value="item.value">
                                        </el-option>
                                    </el-select>
                                </div>
                                <div style="margin: 0 20px;">
                                    <h3>设备名称</h3>
                                    <el-select v-model="valueName" placeholder="请选择">
                                        <el-option v-for="item in optionrole" :key="item.facility_id"
                                            :label="'(' + item.serials + ')' + item.name" :value="item.facility_id">
                                        </el-option>
                                    </el-select>
                                    <el-button type="primary" style="margin-left: 30px;"
                                        :disabled="value == '' || valueName == ''" @click="addprocess">添 加</el-button>
                                </div>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </el-dialog>
            <el-dialog title="返工模板" :visible.sync="dialogVisible" width="40%" :style="{ top: '26%' }">

                <div>
                    <span style="margin: 0 30px;">选择模板</span>
                    <el-select v-model="reworkValue" placeholder="请选择" style="width: 50%;">
                        <el-option v-for="item in reworkList" :key="item.key" :label="item.value" :value="item.key">
                        </el-option>
                    </el-select>
                </div>

                <div style="margin-top: 20px;">
                    <span style="margin: 0 30px;">记录日志</span>
                    <el-input v-model="message" placeholder="请输入要记录的日志" clearable style="width: 50%;" />
                </div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="TemplateDetermination">确 定</el-button>
                </span>
            </el-dialog>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.index" :limit.sync="queryParams.size"
                @pagination="getList" />
        </basic-container>
    </div>
</template>

<script>
import { getrepairlist, eidrepair, dlerepair, gettemplatelist, eidTemplate } from "@/api/image";
import {
    getkindList,

} from "@/api/data";
import { roleStation, } from "@/api/flow";
import { mapGetters } from "vuex";
export default {
    data() {
        return {
            reworkList: [],
            imagelist:[],
            reworkValue: '',
            message: '',
            aircraftId: null,
            tabPosition: 'left',
            loading: false,
            dialogVisible: false,
            valueName: '',
            total: 0,
            queryParams: {
                index: 1,
                size: 10,
                finish: null,
                start: null,
                facility_id: null,
                sequence: null,
            },
          
            optionname: '',
            optionserials: '',
            optionId: '',
            courseId: null,
            optionrole: [],
            options: [
                {
                    value: 0,
                    label: '未加工完成'
                },
                {
                    value: 1,
                    label: '合格'
                },
                {
                    value: 2,
                    label: '不合格'
                }, {
                    value: 3,
                    label: '报废'
                }],
            open: false,
            title: "",
            dataList: [],
            value: '',
            form: {},
            menuList: [],
            rules: {
                aircraft: [
                    { required: true, message: "机种名称不能为空", trigger: "blur" }
                ],
                name: [
                    { required: true, message: "工艺名称不能为空", trigger: "blur" }
                ],
                formula_id: [
                    { required: true, message: "工艺配方不能为空", trigger: "blur" }
                ],

            }
        }
    },
    created() {
        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1;
        var day = today.getDate();
        var formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);
        this.queryParams.start = formattedDate
        this.queryParams.finish = formattedDate
        this.value = [formattedDate, formattedDate]
        this.getList()
        this.getroleStation()
    },
    beforeDestroy() {

    },
    computed: {
        ...mapGetters(["userInfo", "menu"]),
    },
    mounted(){
       this.gitAmagelist()
    },
    methods: {
        /** 查询菜单列表 */
        getList() {
            this.loading = true;
            getrepairlist(this.queryParams).then(response => {
               console.log(response,'0000');
               
                this.menuList = response.data.data
                this.total = response.data.total;
                this.loading = false;
            })
        },
        handleQuery() {
            this.queryParams.start = this.value == null ? null : this.value.length > 0 ? this.value[0] : null
            this.queryParams.finish = this.value == null ? null : this.value.length > 0 ? this.value[1] : null
            this.getList();
        },
        // 返工模板
        handlRework(row) {
            console.log(row);
            this.aircraftId = row.aircraft_id
            this.courseId = null

            this.dialogVisible = true;
            let data = {
                aircraft_id: row.aircraft_id
            }
            gettemplatelist(data).then(res => {
                console.log(res);
                this.reworkList = res.data.data
                this.courseId = row.course_id
            })
        },
        TemplateDetermination() {
            let objData = {
                course_id: this.courseId,
                template_id: this.reworkValue,
                aircraft_id: this.aircraftId,
                message: this.message,
            }
            eidTemplate(objData).then(res => {
                console.log(res);
                this.$message({
                    message: "更新成功",
                    type: "success",
                });
                this.getList()
                this.dialogVisible = false;
            })
        },
        // 获取pLC设备
        getroleStation() {
            roleStation().then(res => {

                this.optionrole = res.data.data
            })
        },
        // 表单重置
        reset() {
            this.form = {
                repairs: [],
                course_id: null,
                mark: null,
                message: null,
                start: null,
                stop: null,

            };
        },
        handleUpdate(row) {

            this.reset()
            this.form = row
            this.dataList = row.repairs.flat()
            this.open = true;
            this.title = "修改工站";
        },

        handleDelete(row) {

            let messsId = {
                course_id: row.course_id
            }
            this.$confirm('是否确认删除名称为"' + row.aircraft + '"的工艺路径？')
                .then(() => {
                    dlerepair(messsId).then(() => {
                        this.$message({
                            message: "删除成功",
                            type: "success",
                        });
                        this.open = false;
                        this.getList();
                        this.reset();
                    });
                })
                .catch(() => { });
        },
        cancel() {
            this.open = false;
            this.getList();
            this.reset();
        },
        handleAdd() {
            this.open = true;
            this.reset();
            this.dataList = []
            this.value = ''
            this.valueName = ''
            this.title = "新增路径";
        },
        addprocess() {
            for (var i = 0; i < this.optionrole.length; i++) {

                if (this.valueName == this.optionrole[i].facility_id) {
                    this.optionserials = this.optionrole[i].serials
                    this.optionname = this.optionrole[i].name
                    this.optionId = this.optionrole[i].facility_id
                }
            }
            for (var j = 0; j < this.dataList.length; j++) {
                if (this.dataList[j].sequence == this.value && this.dataList[j].facility_id == this.optionId) {
                    return this.$message.error('已经有相同步骤了');
                }
            }
            this.dataList.push({
                name: this.optionname,
                serials: this.optionserials,
                sequence: this.value,
                facility_id: this.optionId
            })
            this.value = ''
            this.valueName = ''
            this.dataList.sort(function (a, b) {
                return parseInt(a.sequence) - parseInt(b.sequence);
            })
        },
        handleDatalist(index) {
            this.dataList.splice(index, 1);
        },
        gitAmagelist(){
            getkindList(this.queryParams).then((response) => {
                this.imagelist = response.data.data;
                console.log(this.imagelist,"99999999999");
           
            });

        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    let sequenceMap = {};
                    for (var i = 0; i < this.dataList.length; i++) {
                        let currentSequence = this.dataList[i].sequence;
                        let currentFacilityId = this.dataList[i].facility_id;
                        if (!sequenceMap[currentSequence]) {
                            sequenceMap[currentSequence] = { ft_ids: [] };
                        }
                        sequenceMap[currentSequence].ft_ids.push(currentFacilityId);
                    }
                    this.form.repairs = Object.values(sequenceMap);

                    eidrepair(this.form).then(() => {
                        this.$message({
                            message: "修改成功",
                            type: "success",
                        });
                        this.open = false;
                        this.getList();
                        this.reset();
                    });
                }

            });
        },
        handlePreview(imageUrl) {
            console.log('预览图片数据:', imageUrl); // 调试用
            this.$alert('<img src="data:image/jpeg;base64,' + imageUrl + '" style="width: 100%;">', '图片预览', {
                dangerouslyUseHTMLString: true,
                showConfirmButton: false
            });
        }
    }
};
</script>

<style lang="scss">
.demo-table-expand {
    font-size: 0;

}

.demo-table-expand label {
    width: 90px;
    color: #99a9bf;
}

.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
}

.chengHigh {
    .el-tabs__item {
        height: 60px !important;
        line-height: 60px !important;
        font-size: 16px !important;
        padding: 0 40px !important;
    }

}

.SecondaryContent {
    width: 100%;
    display: flex;

    .ContentItem {
        flex: 1;
    }
}

.Contentarea {
    display: flex;


}

.using {
    font-size: 16px;
    padding: 10px 0;
    width: 50%;

    text-align: center;

}

.parallel {
    font-size: 16px;
    padding: 10px 0;
    width: 50%;

}

.table {
    display: flex;
    justify-content: space-around;
    font-size: 14px;
    line-height: 40px;
    font-weight: 600;
    height: 45px;
    background-color: #f8f8f9;
    color: #515a6e;
    border-bottom: 1px solid #EBEEF5;
}
</style>
